package com.huobao.yunying.activitys;

import java.util.List;
import java.util.Map;

import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.adapters.Adapter_Manager_Address;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.My_address;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.zhangying.R;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
/**
 * 修改收货地址
 * @author Administrator
 *
 */
public class Act_Manager_Address extends HuobaoBaseActivity implements OnItemClickListener{

	@ViewInject(R.id.listview)
	private ListView listview;
	
	private Adapter_Manager_Address adapter;
	private View footview;
	
	private My_address address;
	private boolean isSelectAddress = false;
	@Override
	protected int getContentView() {
		// TODO Auto-generated method stub
		return R.layout.layout_manager_address;
	}

	@Override
	protected String getTitleText() {
		return "取消";
	}

	@Override
	protected void init() {
		isSelectAddress = getIntent().getBooleanExtra("flag", false);
		address = (My_address) getIntent().getSerializableExtra("data");
		footview = LayoutInflater.from(context).inflate(R.layout.item_address_footview, null);
		adapter = new Adapter_Manager_Address(context);
		listview.addFooterView(footview);
		if(isSelectAddress){
			listview.setOnItemClickListener(this);
		}
		listview.setAdapter(adapter);
		getAddress();
		footview.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, Act_Edit_Address.class);
				startActivityForResult(intent, 1);
			}
		});
	}
	
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		if(arg0 == 1 && arg1 == RESULT_OK){
			getAddress();
		}
		super.onActivityResult(arg0, arg1, arg2);
	}

	private void getAddress(){
		Map<String, String> map = HttpUtil.getParams("app","member","act","receiptaddress");
		Http.getInstant().sendRequest(context, map, new HttpListener() {
			
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				List<My_address> list = JSON.parseArray(jsonObject.getString("retval"), My_address.class);
				adapter.clear();
				adapter.addAll(list);
			}
			
			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub
		if(isSelectAddress){
			Intent intent = new Intent();
			intent.putExtra("data", adapter.getItem(position));
			setResult(RESULT_OK, intent);
			finish();
		}
	}
}
